/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "usersparser.h"

#include <QList>
#include <QByteArray>

#include "../Types/user.h"

char parseUser(const QByteArray& userData, int indexData, User& u)
{
    int indexEndOfBlock = userData.indexOf("</a></span>",indexData);
    int indexStartFlag = userData.indexOf("title='",indexData) + strlen("title='");
    int indexEndFlag = userData.indexOf('\'',indexStartFlag);

    if ( indexStartFlag == -1 || indexStartFlag >= indexEndOfBlock )
    {
        u.flag = "";
        indexStartFlag = indexData;
        indexEndFlag = indexData;
    }
    else
    {
        u.flag = userData.mid(indexStartFlag, indexEndFlag-indexStartFlag);
    }

    int indexStartStatus = userData.indexOf("title=\"",indexEndFlag) + strlen("title=\"");
    int indexEndStatus = userData.indexOf('\"',indexStartStatus);

    int indexStartNickname = userData.indexOf(" >",indexEndStatus) + strlen(" >");
    int indexEndNickname = userData.indexOf("</a></span>",indexStartNickname);

    u.status = usFactory(userData.mid(indexStartStatus,indexEndStatus-indexStartStatus));
    u.name = QString::fromUtf8(userData.mid(indexStartNickname, indexEndNickname-indexStartNickname));

    return 0;
}

bool Parser :: readUsersOnline(const QByteArray& pageData, QList<User>& users)
{
    int indexUserData = 0;
    int indexEndOfList = 0;

    qDebug() << "Parser :: readUsersOnline";

    if ( pageData.contains("<h3>Who's Online?</h3>") == false )
    {
        qDebug() << "It looks like the page is not correct";
        return false;
    }

    indexUserData = pageData.indexOf("<div id=\"whosonline\" class=\"whosonline\">");

    if ( indexUserData == -1 )
    {
        qDebug() << "It looks like the page is not correct (second check)";
        return false;
    }

    indexEndOfList = pageData.indexOf("</div>",indexUserData);

    while ( (indexUserData = pageData.indexOf("<span><img src='/static/flags/",indexUserData)) != -1 && indexUserData < indexEndOfList )
    {
        User u;

        parseUser(pageData,indexUserData,u);

        users.push_back(u);

        // Move index to avoid infinite looping
        indexUserData += strlen("<span><img src='/static/flags/");
    }

    qDebug() << users;

    return true;
}
